package com.netease.service;

import com.netease.common.HttpUtil;
import com.netease.common.NetServiceCommon;
import com.netease.domain.MicroserviceDetail;
import com.netease.domain.MicroserviceItem;
import com.netease.domain.RepositoryItem;
import com.sun.tools.javac.jvm.ClassWriter;
import com.sun.tools.javac.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongle on 2017/2/27.
 */
@Service
public class MicroserviceService {
    private static String listUrl = "https://open.c.163.com/api/v1/namespaces/%d/microservices";
    private static String detailUrl = "https://open.c.163.com/api/v1/microservices/%d";
    private static String restartUrl = "https://open.c.163.com/api/v1/microservices/%d/actions/restart?option=%s";
    private static String elasticUrl = "https://open.c.163.com/api/v1/microservices/%d/actions/elastic-scale?new_replicas=%d";
    private static String updateImageUrl = "https://open.c.163.com/api/v1/microservices/%d/actions/update-image";

    @Autowired
    private NetServiceCommon netServiceCommon;
    @Autowired
    private AccessTokenService accessTokenService;

    public List<MicroserviceItem> getListsInNamesapce(Long spaceId) throws IOException {
        List<MicroserviceItem> listAll = netServiceCommon.getListAll(String.format(listUrl, spaceId), MicroserviceItem.class, "microservice_infos", "offset", "limit");
        return listAll;
    }

    public MicroserviceDetail getDetail(Long serviceId) throws IOException {
        MicroserviceDetail detail = netServiceCommon.getResponse(String.format(detailUrl, serviceId), MicroserviceDetail.class);
        return detail;
    }

    public String restartService(Long serviceId, String option) throws IOException {
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Token "+accessTokenService.getToken());

        String ret = HttpUtil.getInstance().commonPut(String.format(restartUrl, serviceId, option), header, null);
        return ret;
    }

    public String elasticScaleService(Long serviceId, Integer newRelplicas) throws IOException {
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Token "+accessTokenService.getToken());

        String ret = HttpUtil.getInstance().commonPut(String.format(elasticUrl, serviceId, newRelplicas), header, null);
        return ret;
    }

    public void updateImage(Long serviceId, Long containerId, String imagePath, Integer updateInterval) throws IOException {
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Token "+accessTokenService.getToken());

        class UpdateImageParam{
            class ContainerImages{
                public Long container_id;
                public String image_path;
            }
            public List<ContainerImages> container_images = new ArrayList<ContainerImages>();
            public Integer min_ready_seconds;
            public UpdateImageParam(){
                container_images.add(new ContainerImages());
            }
        }
        UpdateImageParam param = new UpdateImageParam();
        param.container_images.get(0).container_id = containerId;
        param.container_images.get(0).image_path = imagePath;
        param.min_ready_seconds = updateInterval;

        String ret = HttpUtil.getInstance().commonPut(String.format(updateImageUrl, serviceId), header, param);
    }

}
